using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using UlyssesAgenda.Repositories.Regions;
using Microsoft.ServiceHosting.ServiceRuntime;
using UlyssesAgenda.Repositories;

namespace UlyssesAgenda.Web.Controllers
{
    public class RegionsController : Controller
    {
        private RegionRepository repository;

        public RegionsController() : this(null) { }

        public RegionsController(RegionRepository regionRepository)
        {
            if (regionRepository != null)
                repository = regionRepository;
            else
                repository = RepositoryFactory.GetRepository<RegionRepository>();
        }

        public ActionResult Index()
        {
            RegionFileInfo[] regions;            
            regions = repository.GetRegions();
            return View(regions);
        }

        //
        // GET: /Regions/Details/5

        public ActionResult Details(string id)
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(string id)
        {
        
            repository.DeleteRegion(id);
            return RedirectToAction("Index");
        }

        //
        // GET: /Regions/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Regions/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                RegionFileInfo result = new RegionFileInfo();
                
                string regionName = collection["regionName"];
                string regionDescription = collection["regionDescription"];
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    if (hpf.ContentLength == 0)
                        continue;
                    string fileName = hpf.FileName;
                    result.Name = regionName;
                    result.Description = regionDescription;
                    result.Filename = fileName;
                    result.Submitter = User.Identity.Name;                    
                    result.Url = RoleManager.GetConfigurationSetting("RegionContainer") + "/" + fileName;
                    byte[] fileBytes = new byte[hpf.ContentLength];
                    hpf.InputStream.Read(fileBytes, 0, hpf.ContentLength);
                    repository.CreateRegion(result, hpf.ContentType, fileBytes);
                }

                return View("UploadedRegion", result);                
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Regions/Edit/5
 
        public ActionResult Edit(string id)
        {
            return View();
        }

        //
        // POST: /Regions/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }       
    }
}
